package it.polimi.reTraveldream.ejb.usermanager;



import it.polimi.reTravelDream.dto.UserDTO;
import it.polimi.reTraveldream.entities.City;
import it.polimi.reTraveldream.entities.Prenotation;

import java.io.Serializable;
import java.lang.String;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

import javax.persistence.*;
import javax.xml.bind.DatatypeConverter;

/**
 * Entity implementation class for Entity: Users
 *
 */

@Entity
@Table(name="users")
@NamedQueries({
	@NamedQuery(name=UsersEntity.FIND_ALL,
            query="SELECT u FROM UsersEntity u")	
})
public class UsersEntity implements Serializable {

	private static final long serialVersionUID = 1L;
	public static final String FIND_ALL="UsersEntity.findAll"; 
	
	@Id
	private String email;
	
	private String firstName;
      
    private String lastName;
	
	private String password; 
	
	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date registeredOn;
	
	@ElementCollection(targetClass = Group.class)
	@CollectionTable(name = "users_groups",
            joinColumns = @JoinColumn(name = "email"))
	@Enumerated(EnumType.STRING)
    @Column(name="groupname")
    private List<Group> groups;
	
	public UsersEntity() {
	}   
	
	public UsersEntity(UserDTO userDTO){
		super();
		this.email=userDTO.getEmail();
		this.firstName=userDTO.getFirstName();
		this.lastName=userDTO.getLastName();
		try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] digest = messageDigest.digest(userDTO.getPassword().getBytes());
            this.password = DatatypeConverter.printHexBinary(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        this.registeredOn = new Date();
	}
	
    public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getRegisteredOn() {
		return registeredOn;
	}

	public void setRegisteredOn(Date registeredOn) {
		this.registeredOn = registeredOn;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

}
